import { message } from 'antd';
import { addHoliday, editHoliday, getHolidayDetail } from '@/services/attendance/holidayManagement';

export default {
    namespace: 'addholiday',
    state: {
        holidayDetail: {} // 候选人详情信息
    },
    effects: {
        // 添加假期
        *addHoliday({ payload, success, err }, { call }) {
            const result = yield call(addHoliday, payload);
            if (result.code === 10000) {
                message.success(result.msg);
                // eslint-disable-next-line no-unused-expressions
                success && success();
            } else {
                message.error(result.msg);
                err(result);
            }
        },
        // 编辑假期
        *editHoliday({ payload, success, err }, { call }) {
            const result = yield call(editHoliday, payload);
            if (result.code === 10000) {
                message.success(result.msg);
                // eslint-disable-next-line no-unused-expressions
                success && success();
            } else {
                message.error(result.msg);
                err(result);
            }
            return result;
        },
        // 假期详情信息
        *holidayDetail({ payload }, { call, put }) {
            const result = yield call(getHolidayDetail, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        holidayDetail: result.data
                    }
                });
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
